package BusinessLogic;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import Data.GoodData;
import Data.GoodDataOperator;
import Data.ReturnGoodRecord;
import Data.ReturnGoodRecordOperator;
import Data.SaleRecordData;
import Data.SaleRecordDataOperator;
import Data.UserData;
import Data.UserDataOperator;

public class ReturnModel implements IReturnModel{

	private UserDataOperator uDataOpe = new UserDataOperator();
	private GoodDataOperator gDataOpe = new GoodDataOperator();
	private ArrayList<ReturnItem> returnList = new ArrayList<ReturnItem>();
	private SaleRecordDataOperator srDataOpe = new SaleRecordDataOperator();
	
	public ArrayList<ReturnItem> getReturnList(){
		return returnList;
	}
	
	public void setReturnList(ArrayList<ReturnItem> rList){
		returnList = rList;
	}
	
	public SaleRecordData getSaleRecordDataBySaleID(int SaleID){
		SaleRecordData srd = (SaleRecordData)srDataOpe.readDataByID(SaleID);
		return srd;
	}
	
	public UserDataOperator getUserDataOpe() {
		return uDataOpe;
	}
	
	public void setUserDataOpe(UserDataOperator udo){
		uDataOpe = udo;
	}
	
	public GoodDataOperator getGoodDataOpe(){
		return gDataOpe;
	}
	
	public void setGoodDataOpe(GoodDataOperator gdo){
		gDataOpe = gdo;
	}
	
	public ReturnModel(){
		UserDataOperator uDataOpe = new UserDataOperator();
		GoodDataOperator gDataOpe = new GoodDataOperator();
	    SaleRecordDataOperator srDataOpe = new SaleRecordDataOperator();
	}
	
	public boolean addItem(int SaleID,int GoodID,int GoodNumber,double price,double specialPrice,String date){
		try{
			ReturnItem ri = new ReturnItem(SaleID,GoodID,GoodNumber,price,specialPrice,date);
			returnList.add(ri);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean removeItem(int SaleID,int GoodID,int GoodNumber,double price,double specialPrice,String date){
		try{
			int saleID = SaleID;
			int goodID = GoodID;
			int goodNumber = GoodNumber;
			
			for(ReturnItem ri:returnList){
				if((ri.getGoodID() == goodID)&&(ri.getSaleRecordID() == saleID)&&(ri.getReturnNumber() == goodNumber)&&(ri.getPrice() == price)&&(ri.getSpecialPrice() == specialPrice)&&(ri.getDate() == date)){
					returnList.remove(ri);
					break;
				}
			}
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean removeAllItems(){
		try{
			returnList.clear();
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public double returnGood(){
		double sum = 0;
		for(ReturnItem ri:returnList){
//			sum = ri.getGoodID();
			SaleRecordDataOperator srDataOpe = new SaleRecordDataOperator();
			sum += ri.getGoodPrice() * ri.getReturnNumber();
			srDataOpe.saveData((ri.getSaleRecordID()),(ri.getGoodID()),(ri.getReturnNumber()),(ri.getPrice()),(ri.getSpecialPrice()),(ri.getDate()));
		}
		return sum;
	}
	
	public boolean startUpdate(){
		return false;
	}
	
	public String getGoodDescriptionByID(int ID){
		String goodDescription;
		GoodData gd = (GoodData) gDataOpe.readDataByID(ID);
		goodDescription = gd.getDescription();
		return goodDescription;
	}
	
	public double getGoodPriceByID(int ID){
		double i;
		GoodData gd = (GoodData) gDataOpe.readDataByID(ID);
		i = gd.getPrice();
		return i;
	}
	
	public double getGoodSpecialPriceByID(int ID){
		double i;
		GoodData gd = (GoodData) gDataOpe.readDataByID(ID);
		i = gd.getSpecialPrice();
		return i;
	}
	
	public String getGoodSpecialTimeStartByID(int ID){
		String s;
		GoodData gd = (GoodData) gDataOpe.readDataByID(ID);
		s = gd.getSpecialStart();
		return s;
	}  
	
	public String getGoodSpecialTimeEndByID(int ID){
		String s;
		GoodData gd = (GoodData) gDataOpe.readDataByID(ID);
		s= gd.getSpecialEnd();
		return s;
	}
	
	public double getGoodNumberByID(int ID){
		double d;
		GoodData gd = (GoodData) gDataOpe.readDataByID(ID);
		d = gd.getNumber();
		return d;
	}
	
	public String getUserNameByID(int ID){
		String s;
		UserData ud = (UserData) uDataOpe.readDataByID(ID);
		s = ud.getUserName();
		return s;
	}
	
	public String getUserPasswordByID(int ID){
		String i;
		UserData ud = (UserData) uDataOpe.readDataByID(ID);
		i = ud.getUserPassword();
		return i;
	}
	
	public Double getGoodOriginalPriceByID(int ID){
		SaleRecordDataOperator srd = new SaleRecordDataOperator();
		SaleRecordData sr = srd.readDataByID(ID);
		return sr.getPrice();
	}
	
	public Double getGoodOriginalSpecialPriceByID(int ID){
		SaleRecordDataOperator srd = new SaleRecordDataOperator();
		SaleRecordData sr = srd.readDataByID(ID);
		return sr.getSpecialPrice();
	}
	
	public String getSaleDateByID(int ID){
		SaleRecordDataOperator srd= new SaleRecordDataOperator();
		SaleRecordData sr = srd.readDataByID(ID);
		return sr.getDate();
	}
	
	public int getNextReturnID(){
		ReturnGoodRecordOperator rgro = new ReturnGoodRecordOperator();
		return rgro.getNextReturnID();
	}
	
	public boolean saveReturnGoodRecord(int goodID,String goodName,double returnNumber,double price,double specialPrice,String returnDate,int saleID){
		ReturnGoodRecordOperator rgro = new ReturnGoodRecordOperator();
		rgro.saveData(goodID, goodName, returnNumber, price, specialPrice, returnDate, saleID);
		return true;
	}
	
	public ArrayList<ReturnGoodRecord> readReturnGoodRecordBySaleID(int ID){
		ReturnGoodRecordOperator rgro = new ReturnGoodRecordOperator();
		return rgro.readReturnGoodRecordBySaleID(ID);
	}
}
